import { loadLanguageAsync } from '@/locales'
import zhCN from 'ant-design-vue/lib/locale-provider/zh_CN'
import enUS from 'ant-design-vue/lib/locale-provider/en_US'
// TODO i18n 国际化:如果需要新增其它语言支持, 需要映入对应语言包
import zhTW from 'ant-design-vue/lib/locale-provider/zh_TW'
import { setI18nLocale } from '@/utils/i18nUtil'

const i18nMap = {
  'zh-CN': zhCN,
  'en-US': enUS,
  // TODO i18n 国际化:如果需要新增其它语言支持, 这里也要做相应的映射
  'zh-TW': zhTW
}

const i18n = {
  state: {
    lang: 'en-US',
    locale: enUS
  },
  mutations: {
    SET_LANG: (state, lang) => {
      state.lang = lang
    },
    SET_LOCALE: (state, locale) => {
      state.locale = locale
    }
  },
  actions: {
    // 设置界面语言
    SetLang ({ commit }, lang) {
      setI18nLocale(lang)
      return new Promise(resolve => {
        commit('SET_LANG', lang)
        loadLanguageAsync(lang)
        commit('SET_LOCALE', i18nMap[lang])
        resolve()
      })
    }
  }
}

export default i18n
